-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-144325: Improve error messages for {}-style formatters for int, float, str, and complex
#144326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| def test_invalid_specifier_type_error_message(self): | ||
| for value in [12j, 12, 12.0, "12"]: | ||
| for bad_spec, repr in [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: repr is a built-in.
| for bad_spec, repr in [ | |
| for bad_spec, repr_str in [ |
| ]: | ||
| with self.subTest(value=value, bad_spec=bad_spec): | ||
| err = re.escape("Unknown format code " | ||
| f"{repr} for object of type " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| f"{repr} for object of type " | |
| f"{repr_str} for object of type " |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable.
| if (actual_format_spec != NULL) { | ||
| PyErr_Format(PyExc_ValueError, | ||
| "Invalid format specifier '%U' for object of type '%.200s'", | ||
| "Invalid format specifier %R for object of type '%.200s'", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are here, we can use %T.
| } | ||
| } | ||
|
|
||
| if (end-pos) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are already some checks above. I think that it is better to modify them. Your code emits weird error message for formats ,10, or _10_.
| } | ||
| else if (Py_UNICODE_ISPRINTABLE(conversion)) { | ||
| PyErr_Format(PyExc_ValueError, | ||
| "Unknown conversion specifier '%c' (U+%04X)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such code is now repeated 3 times. Maybe add a helper function? For example, a function that formats "'%c' (U+%04X)" (with special cases).
This is the first implementation of my suggested changes in the issue.
Some proposals are open for discussion, though the PR already includes test cases.
I would appreciate any feedback or suggestions on these proposed changes.
{}style formatters for int, float, str, and complex #144325